package org.et.g4.message;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.eredlab.g4.bmf.base.IDao;
import org.eredlab.g4.bmf.util.SpringBeanLoader;
/**
 * jdbc会话消息处理
 * 2011-03-30
 * @author huangxy 
 * 
 *
 */
public class JdbcMonitorMessageListener implements MessageListener {

	public void setG4Dao(IDao g4Dao) {
		this.g4Dao = g4Dao;
	}
	private IDao g4Dao;
	public void onMessage(Message msg) {
		Connection conn = null;
		try {		
			if (msg instanceof MapMessage && msg.getJMSDestination().equals(SpringBeanLoader.getSpringBean("monitor")))
			{				
				PreparedStatement statement = null;	
				switch(Integer.valueOf(((MapMessage)msg).getString("msgtype")))
				{
					 case 1:
						 String requestSql = "INSERT INTO eaevent(EVENTID,USERID,ACCOUNT,USERNAME,DESCRIPTION,ACTIVETIME,REQUESTPATH,METHODNAME,COSTTIME) values(?,?,?,?,?,?,?,?,?)";
						 conn = g4Dao.getConnection();
						 try {
								statement = conn.prepareStatement(requestSql);
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						statement.setString(1, ((MapMessage)msg).getString("eventid"));
					    statement.setString(2, ((MapMessage)msg).getString("userid"));
					    statement.setString(3, ((MapMessage)msg).getString("account"));
					    statement.setString(4, ((MapMessage)msg).getString("username"));
					    statement.setString(5, ((MapMessage)msg).getString("description"));
					    statement.setString(6, ((MapMessage)msg).getString("activetime"));
					    statement.setString(7, ((MapMessage)msg).getString("requestpath"));
					    statement.setString(8, ((MapMessage)msg).getString("methodname"));
					    statement.setInt(9, Integer.parseInt(((MapMessage)msg).getString("costtime")));
					    statement.executeUpdate();
					    statement.close();
						 break;
					 case 2:break;
					 case 3:
						conn = g4Dao.getConnection();
					    String insertSql = "INSERT INTO eajdbcmonitor(monitorid,sqltext,starttime,costtime,rowcount,type) VALUES(?,?,?,?,?,?)";
					    try {
							statement = conn.prepareStatement(insertSql);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					    statement.setString(1, "1000000000");
					    statement.setString(2, ((MapMessage)msg).getString("sql"));
					    statement.setString(3, ((MapMessage)msg).getString("starttime"));
					    statement.setLong(4, new Long(((MapMessage)msg).getString("time")).longValue());
					    statement.setInt(5, Integer.parseInt(((MapMessage)msg).getString("rows")));
					    statement.setString(6, ((MapMessage)msg).getString("type"));			    
					    statement.executeUpdate();
					    statement.close();
					    break;
				      default :break;
			   }
			}
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			 try {
				if(!conn.equals(null) && !conn.isClosed())
				    	conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
